#include "StartLayer.h"
#include "utility/socket/websocket/WebSocketClient.h"
//#include "game/paopao/LoadingScene.h"
#include "BubbleTouchLayer.h"

bool StartLayer::init()
{
	if (!BaseLayer::init())
	{
		return false;
	}

	m_isFirst = false;
	
	funcBind();

	schedule(schedule_selector(StartLayer::check), 60.0f);

	check(1);

	BubbleTouchLayer* pBubbleTouchLayer = BubbleTouchLayer::create();
	addChild(pBubbleTouchLayer);

	return true;
}

void StartLayer::check(float dt)
{
	if (!m_isFirst) 
	{
		/*string mac =getMacID();
		GlobalData::getInstance()->setMacId(mac.c_str());
		string identityContent = getIdentityContent();
		GlobalData::getInstance()->setIdentity(identityContent.c_str());
		ValueMap param;
		param["terminal_id"] = GlobalData::getInstance()->getIdentity().c_str();
		param["debug"] = false;
		param["version"] = VERSION;
		param["ip"] = " ";
		WebSocketClient::getInstance();*/
	}
}

void StartLayer::funcBind()
{
	m_funcBind = new map<string, TargetFunc>;
	TargetFunc funcIdle = std::bind(&StartLayer::start, this, std::placeholders::_1);
	m_funcBind->insert(make_pair("express-start", funcIdle));
}

void StartLayer::start(void* mess)
{
	m_isFirst = true;
	unschedule(schedule_selector(StartLayer::check));

	//Test Code
	//auto LoadingScene = LoadingScene::createScene("046050bc99"
	//	, (MonsterType)1, 60.0f,1,true,0,0);
	//Director::getInstance()->replaceScene(TransitionCrossFade::create(1, LoadingScene));
	//return;

	auto mainSence = MainScene::create();


	auto startMess = static_cast<StartMessage*>(mess);
	if (startMess->biz_success) 
	{
		const char * csbFileName = "";
		if (0 == strcmp(startMess->csbFileName.c_str(), "AwardScene.csb"))
		{
			csbFileName = "layer/award/AwardChooseLayer.c sb";
		}
		else if (0 == strcmp(startMess->csbFileName.c_str(), "mushroom2.csb"))
		{
			csbFileName = "layer/mushroom/MushroomLayer.csb";
		}
		else if (0 == strcmp(startMess->csbFileName.c_str(), "gameboy.csb"))
		{
			csbFileName = "layer/award/NewForestLayer.csb";
		}
		else
		{
			csbFileName = CCString::createWithFormat("scene/%s", startMess->csbFileName.c_str())->getCString();
		}
		GlobalData::getInstance()->setParkId(startMess->parkId.c_str());
		GlobalData::getInstance()->setTerminalId(startMess->terminalId.c_str());
		mainSence->setDisplay(csbFileName, startMess->layerType.c_str());
		Director::getInstance()->replaceScene(TransitionPageTurn::create(1, mainSence, true));
	}
	else
	{
		Size visibleSize = Director::getInstance()->getVisibleSize();
		Vec2 origin = Director::getInstance()->getVisibleOrigin();

		m_tipLayer->tip(startMess->display.c_str(), Color3B::WHITE);
	}

	startMess->is_delete = true;
	delete mess;
}